﻿Partial Public Class Window
    ''' <summary>
    ''' Represents a collection of child windows belonging to another window.
    ''' </summary>
    Public Class ChildWindowCollection
        Implements IList(Of Window)

        Private listCore As New List(Of Window)
        Private owner As Window

        Protected Friend Sub New(owningWindow As Window)
            owner = owningWindow
        End Sub

        Public ReadOnly Property Count As Integer Implements ICollection(Of Window).Count
            Get
                Return listCore.Count
            End Get
        End Property

        Public ReadOnly Property IsReadOnly As Boolean Implements ICollection(Of Window).IsReadOnly
            Get
                Return False
            End Get
        End Property

        Default Public Property Item(index As Integer) As Window Implements IList(Of Window).Item
            Get
                If index > -1 AndAlso index < listCore.Count Then
                    Return listCore(index)
                End If
                Return Nothing
            End Get
            Set(value As Window)
                If index > -1 AndAlso index < listCore.Count Then
                    If listCore(index) IsNot Nothing Then listCore(index).SetParent(Nothing)
                    value.SetParent(owner)
                    listCore(index) = value
                End If
            End Set
        End Property

        Public Sub Add(item As Window) Implements ICollection(Of Window).Add
            listCore.Add(item)
            item.SetParent(owner)
        End Sub

        Public Sub Clear() Implements ICollection(Of Window).Clear
            For Each entry In listCore
                entry.SetParent(Nothing)
            Next
            listCore.Clear()
        End Sub

        Public Sub CopyTo(array() As Window, arrayIndex As Integer) Implements ICollection(Of Window).CopyTo
            listCore.CopyTo(array, arrayIndex)
        End Sub

        Public Sub Insert(index As Integer, item As Window) Implements IList(Of Window).Insert
            Throw New NotImplementedException()
        End Sub

        Protected Friend Sub BringForward(item As Window)
            Dim index As Integer = listCore.IndexOf(item)
            If index > -1 AndAlso index < listCore.Count - 1 Then
                listCore.RemoveAt(index)
                listCore.Insert(index + 1, item)
            End If
        End Sub

        Public Sub RemoveAt(index As Integer) Implements IList(Of Window).RemoveAt
            If index > -1 AndAlso index < listCore.Count Then
                listCore.Item(index).SetParent(Nothing)
                listCore.RemoveAt(index)
            End If
        End Sub

        Public Function Contains(item As Window) As Boolean Implements ICollection(Of Window).Contains
            Return listCore.Contains(item)
        End Function

        Public Function GetEnumerator() As IEnumerator(Of Window) Implements IEnumerable(Of Window).GetEnumerator
            Return listCore.GetEnumerator
        End Function

        Public Function IndexOf(item As Window) As Integer Implements IList(Of Window).IndexOf
            Return listCore.IndexOf(item)
        End Function

        Public Function Remove(item As Window) As Boolean Implements ICollection(Of Window).Remove
            Dim result = listCore.Remove(item)
            If result Then item.SetParent(Nothing)
            Return result
        End Function

        Private Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
            Return listCore.GetEnumerator
        End Function
    End Class
End Class
